package com.hmkj.admin.controller.fqa;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.fqa.FqaCategory;
import com.hmkj.core.service.fqa.FqaCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class CotegoryJsonController extends GenericController<Integer, FqaCategory> {
    @Resource
    private FqaCategoryService fqaCategoryService;
    /**
     * 客户列表列表json数据
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("category/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return fqaCategoryService.getDtGridList(dtGridPager, FqaCategory.class);
    }

    /**
     * 新增问题分类
     *
     * @param name
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("category/json/add")
    @RequiresPermissions("sys:category:save")
    public ResultEntity addJson(String name) throws Exception {
        checkModel(name);
        if (fqaCategoryService.selectCount(new FqaCategory(t -> {
            t.setName(name);
        }))>0) {
            throw new BussinessException("分类名已存在");
        }
        fqaCategoryService.insert(new FqaCategory(t -> {
            t.setName(name);
        }));
        return ok();
    }

    /**
     * 编辑问题分类
     *
     * @param name
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("category/json/edit")
    @RequiresPermissions("sys:category:edit")
    public ResultEntity editJson(Integer id, String name) throws Exception {
        FqaCategory fqa = checkPrimaryKey(id);
        checkModel(name);
        if (!fqa.getName().equals(name)) {
            if (fqaCategoryService.selectCount(new FqaCategory(t -> {
                t.setName(name);
            })) > 0) {
                throw new BussinessException("分类名已存在");
            }
            fqaCategoryService.updateByPrimaryKeySelective(new FqaCategory(t -> {
                t.setId(id);
                t.setName(name);
            }));
        }
        return ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("category/json/del")
    @RequiresPermissions("sys:category:del")
    public ResultEntity del(Integer id) throws Exception {
        FqaCategory fqaCategory=checkPrimaryKey(id);
        fqaCategoryService.updateByPrimaryKeySelective(new FqaCategory(a -> {
            a.setId(fqaCategory.getId());
            a.setDelFlag(FqaCategory.DELFLAG.DELETED.code);
        }));
        return ok();
    }

    private void checkModel(String name) {
        if (!StringUtils.isNotBlank(name)) {
            throw new BussinessException("分类名称不能为空");
        } else if (name.length() > 11) {
            throw new BussinessException("分类名称长度不能超过11位");
        }
    }

    private FqaCategory checkPrimaryKey(Integer id) throws Exception {
        if (null == id) {
            throw new BussinessException("参数有误");
        } else {
            FqaCategory fqa = fqaCategoryService.selectByPrimaryKey(id);
            if (null != fqa) {
                return fqa;
            } else {
                throw new BussinessException("参数有误");
            }
        }
    }
}
